package AtomicDemo.AtomicLongDemo;

import java.util.Random;

/**
 * @Author: ylc
 * @Description:模拟服务器的请求总数，记录成功数与失败数
 * @Date Created in 2022-05-14 15:32
 */
public class AtomicLongDemo {
    public static void main(String[] args) throws Exception{
        // 通过线程模拟请求
        for (int i = 0; i < 10000; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    //每个线程都是一个请求
                    Indicator.getInstance().newRequestReceive();
                    int num = new Random().nextInt();
                    if (num %2 == 0){
                        //偶数模拟成功
                        Indicator.getInstance().requestSuccess();
                    }else {Indicator.getInstance().requestFialure();}
                }
            }).start();
        }
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //打印结果
        System.out.println("请求的总数--->"+Indicator.getInstance().getRequestCount());
        System.out.println("请求成功数--->"+Indicator.getInstance().getRequestSuccess());
        System.out.println("请求失败数--->"+Indicator.getInstance().getRequestFialure());


    }
}
